package com.ant.storage.service.impl;

import com.ant.storage.common.ResultPage;
import com.ant.storage.mapper.InventoryMapper;
import com.ant.storage.mapper.LossReportingMapper;
import com.ant.storage.pojo.LossReporting;
import com.ant.storage.pojo.LossReportingCommodity;
import com.ant.storage.service.ICityService;
import com.ant.storage.service.ILossReportingService;
import com.ant.storage.utils.NullInspectUtil;
import com.ant.storage.common.PageRecv;
import com.ant.storage.common.ResultVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LossReportingServiceImpl extends ServiceImpl<LossReportingMapper,LossReporting> implements ILossReportingService {

    @Autowired(required = false)
    LossReportingMapper lossReportingMapper;

    @Autowired(required = false)
    InventoryMapper inventoryMapper;

    @Autowired
    ICityService cityService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultVo addLossReporting(LossReporting lossReporting) throws NoSuchFieldException {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        lossReporting.setLoss_reporting_time(simpleDateFormat.format(date));
        String str = NullInspectUtil.ClassnullInspect(lossReporting,"loss_reporting_id","city","dept","start_time","end_time");
        if(str != null){
            NullInspectUtil.myAnnotationError(lossReporting,str);
        }
        if(this.save(lossReporting)){
            for (LossReportingCommodity lossReportingCommodity:lossReporting.getList()){
                //中间表插入
                lossReportingMapper.addLossReportingCommodity(lossReporting.getLoss_reporting_id(),lossReportingCommodity.getCommodity().getCommodityId(),lossReportingCommodity.getLoss_reporting_quantity(),lossReportingCommodity.getWarehouse().getId());
                //扣减对应库存
                inventoryMapper.stockSubtract(lossReportingCommodity.getWarehouse().getId(),lossReportingCommodity.getCommodity().getCommodityId(),lossReportingCommodity.getLoss_reporting_quantity());
            }
        }
        return ResultVo.success("ok");
    }

    @Override
    public ResultPage queryLossReporting(PageRecv<LossReporting> pageRecv) {
        Map map = new HashMap<>();
        if (!(pageRecv.getData()==null)){
            map = NullInspectUtil.ClassChangeMapNotNull(pageRecv.getData());
        }
        Integer page = (pageRecv.getCurrentPage()-1)*pageRecv.getPageSize();
        Integer limit = pageRecv.getCurrentPage()*pageRecv.getPageSize();
        List<LossReporting> list = lossReportingMapper.queryAllocation(page,limit,map);
//        for (LossReporting lossReporting:list){
//            lossReporting.getCity().setName(cityService.idQuery(lossReporting.getCity_id()));
//            lossReporting.getDept().setName(cityService.idQuery(lossReporting.getDept_id()));
//        }
        Integer count = lossReportingMapper.countReportingMapper(map);
        return ResultPage.success("ok",list,count);
    }
}
